package ynu.edu.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ynu.edu.entity.DeliveryAddress;
import ynu.edu.service.DeliveryAddressService;
import ynu.edu.dto.CommonResponse;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/DeliveryAddressController")
public class DeliveryAddressController {

    @Autowired
    private DeliveryAddressService deliveryAddressService;

    @PostMapping("/listDeliveryAddressByUserId")
    public ResponseEntity<?> listDeliveryAddressByUserId(@RequestBody DeliveryAddress params) {
        log.info("Received listDeliveryAddressByUserId request with params: {}", params);
        try {
            if (params.getUserName() == null) {
                return ResponseEntity.badRequest().body(new CommonResponse(false, "userName不能为空"));
            }
            List<DeliveryAddress> addresses = deliveryAddressService.listByUserName(params.getUserName());
            return ResponseEntity.ok(new CommonResponse(true, "查询成功", addresses));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new CommonResponse(false, e.getMessage()));
        }
    }

    @PostMapping("/getDeliveryAddressById")
    public ResponseEntity<?> getDeliveryAddressById(@RequestBody DeliveryAddress params) {
        log.info("Received getDeliveryAddressById request with params: {}", params);
        try {
            DeliveryAddress address = deliveryAddressService.getById(params.getDaId());
            return ResponseEntity.ok(new CommonResponse(true, "查询成功", address));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new CommonResponse(false, e.getMessage()));
        }
    }

    @PostMapping("/updateDeliveryAddress")
    public ResponseEntity<?> updateDeliveryAddress(@RequestBody DeliveryAddress params) {
        log.info("Received updateDeliveryAddress request with params: {}", params);
        try {
            int result = deliveryAddressService.updateAddress(params);
            return ResponseEntity.ok(new CommonResponse(result == 1, result == 1 ? "更新成功" : "更新失败"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new CommonResponse(false, e.getMessage()));
        }
    }

    @PostMapping("/removeDeliveryAddress")
    public ResponseEntity<?> removeDeliveryAddress(@RequestBody DeliveryAddress params) {
        log.info("Received removeDeliveryAddress request with params: {}", params);
        try {
            int result = deliveryAddressService.removeAddress(params.getDaId());
            return ResponseEntity.ok(new CommonResponse(result == 1, result == 1 ? "删除成功" : "删除失败"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new CommonResponse(false, e.getMessage()));
        }
    }

    @PostMapping("/saveDeliveryAddress")
    public ResponseEntity<?> saveDeliveryAddress(@RequestBody DeliveryAddress params) {
        log.info("Received saveDeliveryAddress request with params: {}", params);
        if (params.getUserName() == null) {
            return ResponseEntity.badRequest().body(new CommonResponse(false, "userName不能为空"));
        }
        try {
            int result = deliveryAddressService.saveAddress(params);
            return ResponseEntity.ok(new CommonResponse(result == 1, result == 1 ? "保存成功" : "保存失败"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new CommonResponse(false, e.getMessage()));
        }
    }
} 